package com.bidevalution.controller.task;

import com.bidevalution.business.service.*;
import com.bidevalution.dao.entity.BeSectionInfo;
import com.bidevalution.support.context.Constants;
import com.bidevalution.support.context.SmsTemplateCode;
import com.ggzy.tongyi.dubbo.api.EnterPriseInfoDubboService;
import com.google.common.collect.Maps;
import com.qtp.core.model.Response;
import com.qtp.core.redis.RedisUtil;
import com.qtp.core.util.DateUtil;
import com.qtp.core.util.GsonUtil;
import com.qtp.core.util.OConvertUtils;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author shenh@glodon.com
 * @date 2019-07-08 11:06
 */
//@Component
@Log4j2
public class SmsSendTask {

    @Autowired
    private BeSectionInfoService beSectionInfoService;

    @Autowired
    private SmsSendService smsSendService;

    @Autowired
    private EnterPriseInfoDubboService enterPriseInfoDubboService;

    @Autowired
    private BeSectionParamsService beSectionParamsService;
    @Autowired
    private BeSectionDecryptService beSectionDecryptService;
    @Autowired
    private BeSectionSignService beSectionSignService;
    /**
     * 1分钟执行一次
     *
     * @throws Exception
     */
    //@Scheduled(cron = "0 */1 *  * * * ")
    public void initData() throws Exception {
        List<BeSectionInfo> beSectionInfoList = beSectionInfoService.getTodaySectionList();
        Map<String, Object> paramMap = null;
        for(BeSectionInfo beSectionInfo : beSectionInfoList){
            if (beSectionInfo != null) {
                String sectionId = beSectionInfo.getSeqId();
                String bidOpenTime = DateUtil.dateToString(beSectionInfo.getBidOpenTime(),DateUtil.PT2);
                //获取参数信息
                paramMap = Maps.newHashMap();
                paramMap.put("sectionName", beSectionInfo.getSectionName());
                paramMap.put("bidOpenTime", bidOpenTime);
                String data = GsonUtil.toJsonWithGson(paramMap);
                //提前2小时的短信发送
                if(!(bidOpenTime).equals(RedisUtil.get(sectionId+SmsTemplateCode.kb0001.getValue()))){
                    if(DateUtil.getAfterHour(beSectionInfo.getBidOpenTime(), -2).getTime() <= DateUtil.getCurrentTime()){
                        String phones = smsSendService.getAgentPhones(sectionId) + Constants.COMMA + smsSendService.getBidPhones(sectionId);
                        if(smsSendService.sendMsg(SmsTemplateCode.kb0001.getValue(), phones, data)) {
                            RedisUtil.set(sectionId + SmsTemplateCode.kb0001.getValue(), bidOpenTime);
                            //设置key值24小时过期
                            RedisUtil.expire(sectionId + SmsTemplateCode.kb0001.getValue(), 24 * 60 * 60);
                        }
                    }
                }
                //提前10分钟的短信发送
                if(!(bidOpenTime).equals(RedisUtil.get(sectionId+SmsTemplateCode.kb0002.getValue()))){
                    if(DateUtil.getAfterMinute(beSectionInfo.getBidOpenTime(), -10).getTime() <= DateUtil.getCurrentTime()){
                        String phones = smsSendService.getAgentPhones(sectionId) + Constants.COMMA + smsSendService.getBidPhones(sectionId);
                        if(smsSendService.sendMsg(SmsTemplateCode.kb0002.getValue(), phones, data)) {
                            RedisUtil.set(sectionId + SmsTemplateCode.kb0002.getValue(), bidOpenTime);
                            //设置key值24小时过期
                            RedisUtil.expire(sectionId + SmsTemplateCode.kb0002.getValue(), 24 * 60 * 60);
                        }
                    }
                }

                //开标开始短信发送
                if(!(bidOpenTime).equals(RedisUtil.get(sectionId+SmsTemplateCode.kb0004.getValue()))){
                    if(beSectionInfo.getBidOpenTime().getTime() <= DateUtil.getCurrentTime()){
                        Map<String, Object> params = new HashMap<>();
                        params.put("ep_org_code", beSectionInfo.getAgentOrgCode());
                        // 获取请求中的参数
                        Response<Map<String, Object>> response = enterPriseInfoDubboService.getEnterInfo(params);
                        paramMap.put("agentName", response.getResult().get("ep_name"));
                        String phones = smsSendService.getBidPhones(sectionId);
                        if(smsSendService.sendMsg(SmsTemplateCode.kb0004.getValue(), phones, GsonUtil.toJsonWithGson(paramMap))) {
                            RedisUtil.set(sectionId + SmsTemplateCode.kb0004.getValue(), bidOpenTime);
                            //设置key值24小时过期
                            RedisUtil.expire(sectionId + SmsTemplateCode.kb0004.getValue(), 24 * 60 * 60);
                        }
                    }
                }

                //解密即将截止,截止前10分钟发送提醒
                if(!(bidOpenTime).equals(RedisUtil.get(sectionId+SmsTemplateCode.kb0007.getValue()))
                        &&!(bidOpenTime).equals(RedisUtil.get(sectionId+SmsTemplateCode.kb0009.getValue()))){
                    String decryptEndTime = beSectionParamsService
                            .getBeSectionParamsByCode(beSectionInfo.getSeqId(), "decryptEndTime");
                    if(DateUtil.getAfterMinute(DateUtil.stringToDate(decryptEndTime,DateUtil.PT2), -10).getTime() <= DateUtil.getCurrentTime()){
                        String phones = smsSendService.getBidPhones(sectionId);
                        smsSendService.sendMsg(SmsTemplateCode.kb0007.getValue(), phones, data);
                        RedisUtil.set(sectionId+SmsTemplateCode.kb0007.getValue(),bidOpenTime);
                        //设置key值24小时过期
                        RedisUtil.expire(sectionId+SmsTemplateCode.kb0007.getValue(),24 * 60 * 60);
                    }
                }

                //解密截止或提前解密完成,发送提醒
                if(!(bidOpenTime).equals(RedisUtil.get(sectionId+SmsTemplateCode.kb0008.getValue()))
                        &&!(bidOpenTime).equals(RedisUtil.get(sectionId+SmsTemplateCode.kb0009.getValue()))){
                    String decryptEndTime = beSectionParamsService
                            .getBeSectionParamsByCode(beSectionInfo.getSeqId(), "decryptEndTime");
                    if(DateUtil.stringToDate(decryptEndTime,DateUtil.PT2).getTime() <= DateUtil.getCurrentTime()){
                        //解密截止时间已到
                        String phones = smsSendService.getAgentPhones(sectionId);
                        int finishDecryptCount = beSectionDecryptService.getDecryptedCount(sectionId).intValue();
                        int signCount = beSectionSignService.querySignCount(sectionId).intValue();
                        int unFinishDecryptCount = signCount - finishDecryptCount;
                        paramMap.put("finishDecryptCount", finishDecryptCount);
                        paramMap.put("signCount", signCount);
                        paramMap.put("unFinishDecryptCount", unFinishDecryptCount);
                        if(smsSendService.sendMsg(SmsTemplateCode.kb0008.getValue(), phones, GsonUtil.toJsonWithGson(paramMap))) {
                            RedisUtil.set(sectionId + SmsTemplateCode.kb0008.getValue(), bidOpenTime);
                            //设置key值24小时过期
                            RedisUtil.expire(sectionId + SmsTemplateCode.kb0008.getValue(), 24 * 60 * 60);
                        }
                    } else if(beSectionInfo.getBidOpenTime().getTime() <= DateUtil.getCurrentTime()){
                        //提前解密完成
                        String phones = smsSendService.getAgentPhones(sectionId);
                        int finishDecryptCount = beSectionDecryptService.getDecryptedCount(sectionId).intValue();
                        int signCount = beSectionSignService.querySignCount(sectionId).intValue();
                        paramMap.put("finishDecryptCount", finishDecryptCount);
                        paramMap.put("signCount", signCount);
                        if(finishDecryptCount == signCount){
                            if(smsSendService.sendMsg(SmsTemplateCode.kb0009.getValue(), phones, GsonUtil.toJsonWithGson(paramMap))) {
                                RedisUtil.set(sectionId + SmsTemplateCode.kb0009.getValue(), bidOpenTime);
                                //设置key值24小时过期
                                RedisUtil.expire(sectionId + SmsTemplateCode.kb0009.getValue(), 24 * 60 * 60);
                            }
                        }
                    }
                }
            }
        }

    }

}
